package com.jm.web.builder;


import java.io.IOException;

import com.jm.utils.PageJsGear;
import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.web.BasePage;
import com.jm.web.form.Button;
import com.jm.web.form.Toolbar;
import com.jm.web.tags.toolbar.ToolBarTag;


/**
 * @author yanjie
 *
 * 2011/9/19 上午10:37:02
 * 工具栏绘制器
 */
public class ToolbarBuilder extends BaseBuilder{
	protected ToolBarTag tbTag = null ;
	
	@Override
	public void RenderEnd(){
		if(this.getBaseTag() instanceof ToolBarTag)
			this.tbTag = (ToolBarTag)this.getBaseTag();
		
		if(this.tbTag.getToolbar().isEnab()){
			//绘制工具栏
			super.RenderEnd();
			//註冊工具欄按鍵事件
			//this.getBaseTag().getPage().RegisterCmdScript(BasePage.CMD_TOOLBAR_BUTTON_JS); //2012/03/08 取消 改為直接生成
			
			StringBuilderEx sb = new StringBuilderEx();
			try {
				//sb.Append("<div id=\"ol_loading_mask\" class=\"ol_loading_mask\" style=\"width:90%%;\"></div>");
				//sb.Append("<div id=\"ol_loading\" class=\"ol_loading\" style=\"font-size:12px;padding-top:70px;width:90%%;\">[<span id=\"loadingtext\" style=\"font-weight:bold;color:blue;\">頁面</span>] %1$s...</div>",this.getBaseTag().getPage().getUtils().i18n("加載中"));
				sb.Append("<script>");
				
				sb.Append(PageJsGear.getLogBebinJs(this.getBaseTag().getPage(),"toolbar"));
				sb.Append(PageJsGear.getToolbarButtonJs(this.getBaseTag().getPage()));
				sb.Append(PageJsGear.getLogEndJs(this.getBaseTag().getPage(),"toolbar"));
				
				sb.Append("</script>");
				
				this.getOut().write(sb.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	@Override
	protected void OutputTagBegin()  {
		super.OutputTagBegin();
		StringBuilderEx sb = new StringBuilderEx();
		sb.Append("<table id='toolbar' class='toolbar' width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\" height=\"23\" ><tbody><tr><td height=\"100%\" class=\"toobar\" align=\"center\" valign=\"middle\"><table width=\"100%\" height=\"100%\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\"><tbody><tr valign=\"middle\">");
		try {
			this.getOut().write(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	protected void OutputTagBody()  {
		try {
			this.getOut().write("<td width=\"2px\">&nbsp;</td>");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.tbTag.getToolbar().onRender(this.getOut());
	}
	
	@Override
	protected void OutputTagEnd() {
		StringBuilderEx sb = new StringBuilderEx();
		//filter按钮
		Button btn2 = null;
		if(this.getBaseTag().getPage().isFilterEnab()){
			btn2 =Toolbar.getNewButton(this.getBaseTag().getPage().getUtils().i18n("篩選"),"filter","filter","sx-tb-filter",this.getBaseTag());
			btn2.setStyle("width:auto;font-weight:bold;float:right;border:0;");
			
			if(this.getBaseTag().getPage().isSpOpen()){
				btn2.appendCss("ax-btn-pressed");
			}
			this.getBaseTag().getPage().RegisterCmdScript(BasePage.CMD_TOOLBAR_BUTTON_FILTER_JS);
		}
		
		sb.Reset();
		sb.Append("<td width=\"100%\"  style=\"border:0px !important;padding-right:3px;text-align: right;width:100%;align:right;\" class=\"toobar-td-cell\">");
		//sb.Append(btn.toOutString());
		
		try {
			this.getOut().write(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(btn2 != null)
			btn2.onRender(this.getOut());
		
		sb.Reset();
		sb.Append("</td>");
		sb.Append("</tr></tbody></table></td></tr></tbody></table>");
		try {
			this.getOut().write(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//處理按鈕的confirm
		sb.Reset();
		if(this.tbTag.GetExtValue("newconfirm",false)){
			sb.Append("$P.confirm['"+this.tbTag.GetExtValue("newaction","new")+"-"+this.tbTag.GetExtValue("newactionvalue","")+"']='%1$s';",this.getBaseTag().getPage().getUtils().i18n(this.tbTag.GetExtValue("newconfirmText","新增")));
		}
		if(this.tbTag.GetExtValue("searchconfirm",false)){
			sb.Append("$P.confirm['"+this.tbTag.GetExtValue("searchaction","search")+"-"+this.tbTag.GetExtValue("searchactionvalue","")+"']='%1$s';",this.getBaseTag().getPage().getUtils().i18n(this.tbTag.GetExtValue("searchconfirmText","查詢")));
		}
		if(this.tbTag.GetExtValue("saveconfirm",false)){
			sb.Append("$P.confirm['"+this.tbTag.GetExtValue("saveaction","save")+"-"+this.tbTag.GetExtValue("saveactionvalue","")+"']='%1$s';",this.getBaseTag().getPage().getUtils().i18n(this.tbTag.GetExtValue("saveconfirmText","儲存")));
		}
		if(this.tbTag.GetExtValue("cancelconfirm",false)){
			sb.Append("$P.confirm['"+this.tbTag.GetExtValue("cancelaction","cancel")+"-"+this.tbTag.GetExtValue("cancelactionvalue","")+"']='%1$s';",this.getBaseTag().getPage().getUtils().i18n(this.tbTag.GetExtValue("cancelconfirmText","取消")));
		}
		if(this.tbTag.GetExtValue("deleteconfirm",true)){
			sb.Append("$P.confirm['"+this.tbTag.GetExtValue("deleteaction","delete")+"-"+this.tbTag.GetExtValue("deleteactionvalue","")+"']='%1$s';",this.getBaseTag().getPage().getUtils().i18n(this.tbTag.GetExtValue("deleteconfirmText","刪除")));
		}
		if(this.tbTag.GetExtValue("copyconfirm",false)){
			sb.Append("$P.confirm['"+this.tbTag.GetExtValue("copyaction","copy")+"-"+this.tbTag.GetExtValue("copyactionvalue","")+"']='%1$s';",this.getBaseTag().getPage().getUtils().i18n(this.tbTag.GetExtValue("copyconfirmText","複製")));
		}
		if(this.tbTag.GetExtValue("startconfirm",false)){
			sb.Append("$P.confirm['"+this.tbTag.GetExtValue("startaction","start")+"-"+this.tbTag.GetExtValue("startactionvalue","")+"']='%1$s';",this.getBaseTag().getPage().getUtils().i18n(this.tbTag.GetExtValue("startconfirmText","送簽")));
		}
		if(this.tbTag.GetExtValue("checkconfirm",false)){
			sb.Append("$P.confirm['"+this.tbTag.GetExtValue("checkaction","check")+"-"+this.tbTag.GetExtValue("checkactionvalue","")+"']='%1$s';",this.getBaseTag().getPage().getUtils().i18n(this.tbTag.GetExtValue("checkconfirmText","審核")));
		}
		if(this.tbTag.GetExtValue("exportconfirm",false)){
			sb.Append("$P.confirm['"+this.tbTag.GetExtValue("exportaction","export")+"-"+this.tbTag.GetExtValue("exportactionvalue","")+"']='%1$s';",this.getBaseTag().getPage().getUtils().i18n(this.tbTag.GetExtValue("exportconfirmText","導出")));
		}
		if(this.tbTag.GetExtValue("printconfirm",false)){
			sb.Append("$P.confirm['print-']='%1$s';",this.getBaseTag().getPage().getUtils().i18n(this.tbTag.GetExtValue("printconfirmText","列印")));
		}
		if(this.tbTag.GetExtValue("helpconfirm",false)){
			sb.Append("$P.confirm['help-']='%1$s';",this.getBaseTag().getPage().getUtils().i18n(this.tbTag.GetExtValue("helpconfirmText","幫助")));
		}
		
		this.getBaseTag().getPage().RegisterOnReadyScript(BasePage.SCRIPTRUN_ONREADY,sb.toString());
	}
}
